package com.hitqz.robot.biz.util;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.FileAppender;
import cn.hutool.core.io.IoUtil;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件工具
 *
 * @author xupkun
 * @date 2024/12/4
 */
public class CommonFileUtil {


    public static String zipLogFile() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        FileAppender fileAppender = (FileAppender) loggerContext.getLogger("ROOT").getAppender("info");
        if (fileAppender!= null) {
            String logFilePath = System.getProperty("user.dir") + File.separator + fileAppender.getFile().replace("/info.log", "");
            String targetLog = System.getProperty("java.io.tmpdir") + File.separator + "log.zip";
            new File(targetLog).deleteOnExit();
            CommonFileUtil.compressFolderWithCommonsCompress(logFilePath, targetLog);
            return targetLog;
        }
        return null;
    }


    public static void compressFolderWithCommonsCompress(String sourceFolderPath, String zipFilePath) {
        try (ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(new FileOutputStream(zipFilePath))) {
            zipOut.setUseZip64(Zip64Mode.AsNeeded);
            compressFolderHelperWithCommonsCompress(sourceFolderPath, "", zipOut);
            zipOut.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void compressFolderHelperWithCommonsCompress(String folderPath, String parentPath, ZipArchiveOutputStream zipOut) throws IOException {
        File folder = new File(folderPath);
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files!= null) {
                for (File file : files) {
                    String filePath = parentPath + file.getName() + "/";
                    if (file.isDirectory()) {
                        ZipArchiveEntry zipEntry = new ZipArchiveEntry(filePath);
                        zipOut.putArchiveEntry(zipEntry);
                        zipOut.closeArchiveEntry();
                        compressFolderHelperWithCommonsCompress(file.getAbsolutePath(), filePath, zipOut);
                    } else {
                        compressFileWithCommonsCompress(file, parentPath, zipOut);
                    }
                }
            }
        }
    }

    private static void compressFileWithCommonsCompress(File file, String parentPath, ZipArchiveOutputStream zipOut) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, parentPath + file.getName());
        zipOut.putArchiveEntry(zipEntry);
        IoUtil.copy(fis, zipOut);
        zipOut.closeArchiveEntry();
        fis.close();
    }


}
